package com.ccsc.auth.utils;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;

/**
 * Created by XingWen on 2016/11/17.
 */
public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private String[] encryptPropNames = { "jdbc_username_auth", "jdbc_password_auth" };

    private Resource[] locations;

    public void setLocations(Resource[] locations) {
        this.locations = locations;
    }

    protected String convertProperty(String propertyName, String propertyValue) {
        try {
            if (isEncryptProp(propertyName)) {
                String decryptValue = EncryptUtil.decrypt(propertyValue);
                return decryptValue;
            } else {
                return propertyValue;
            }
        }
        catch (Exception ex)
        {
            return propertyValue;
        }
    }

    /**
     * 判断是否是加密的属性
     *
     * @param propertyName
     * @return
     */
    private boolean isEncryptProp(String propertyName) {
        for (String encryptpropertyName : encryptPropNames) {
            if (encryptpropertyName.equals(propertyName))
                return true;
        }
        return false;
    }
}
